<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 250px; margin-bottom: 5px"
        v-model="search.sale_identifier"
        placeholder="请输入销售方纳税人识别码"
        clearable
      ></el-input>
      <el-input
        style="width: 250px; margin-bottom: 5px"
        v-model="search.buy_identifier"
        placeholder="请输入购买方纳税人识别号"
        clearable
      ></el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>

      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-upload
        class="upload-demo"
        style="display: inline; margin-left: 10px"
        action="#"
        :show-file-list="false"
        :http-request="handleAvatarSuccess"
      >
        <el-button
          size="small"
          type="primary"
          class="handleSearch"
          icon="el-icon-circle-plus"
          >导入发票</el-button
        >
      </el-upload>
      <el-button
        size="small"
        type="primary"
        @click="add"
        icon="el-icon-circle-plus"
        >添加发票</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="invoice_num"
        label="发票号码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="invoice_code"
        label="发票代码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="not_tax_amount"
        label="不含税金额"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="billing_date"
        label="开票日期"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="check_code"
        label="校验码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sale_identifier"
        label="销售方纳税人识别号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sale_name"
        label="销售方名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sale_bank_and_account"
        label="销售方开户行及帐号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="sale_address_and_tel"
        label="销售方地址、电话"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buy_identifier"
        label="购买方纳税人识别号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="buy_name"
        label="购买方名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="price_in_words"
        label="价税合计大写"
        align="center"
      ></el-table-column>
      <el-table-column
        width="200"
        prop="goods_services_name"
        label="货物或服务名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="amount_detailed"
        label="金额明细"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tax_rate_detailed"
        label="税率明细"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="tax_amount_detailed"
        label="税额明细"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="ticket_type"
        label="专票/普票"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="quantity"
        label="数量明细"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit_price"
        label="单价明细"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="invoice_type"
        label="发票类型"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="details(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <addinvoiceDialog :dialog="dialog" @upData="getData"></addinvoiceDialog>
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import AddinvoiceDialog from "@/components/invoiceDialog/addInvoiceDialog";
import { invoiceList } from "@/api/invoice/invoiceList";
import { getToken } from "@/utils/auth";
import { Loading } from "element-ui";
export default {
  components: {
    pagination: Pagination,
    addinvoiceDialog:AddinvoiceDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },

      date: [],
      search: {
        buy_identifier: "",
        sale_identifier: "",
        create_date: "",
        end_date: "",
      },
      dialog: {
        centerDialogVisible: false,
      },
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        sale_identifier: this.search.sale_identifier,
        buy_identifier: this.search.buy_identifier,
        start: this.search.create_date,
        end: this.search.end_date,
      };
      invoiceList(data)
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      if (this.date.length > 0) {
        this.search.create_date = this.date[0];
        this.search.end_date = this.date[1];
      }
      this.getData();
    },
    //搜索清除
    empty() {
      this.search = {
        buy_identifier: "",
        sale_identifier: "",
        create_date: "",
        end_date: "",
      };
      this.date = [];
      this.pageData.page = 1;
    },
    // 添加发票
    add(scope) {
      this.dialog.centerDialogVisible = true;
    },
    handleAvatarSuccess(param) {
      let loading = Loading.service({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.1)",
      });
      let data = new window.FormData();
      data.append("file", param.file);
      data.append("token", getToken());
      this.$axios.post("/smkapi/invoice/importInvoice", data).then((res) => {
        loading.close();
        if (res.data.code == 0) {
          this.$message({
            type: "success",
            message: res.data.message || res.data.msg,
          });
          this.pageData.page = 1;
          this.getData();
        } else {
          this.$message({
            type: "info",
            message: res.data.message || res.data.msg,
          });
        }
      });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>